package com.company.cloud.user.modules.demo.controller;

import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSON;
import com.alicp.jetcache.Cache;
import com.alicp.jetcache.anno.CacheType;
import com.alicp.jetcache.anno.CreateCache;
import com.alicp.jetcache.support.FastjsonKeyConvertor;
import com.company.cloud.common.bean.BaseResult;
import com.company.cloud.user.modules.demo.models.CacheTestModel;
import com.company.cloud.user.modules.demo.service.JetCacheTestService;
import com.company.common.server.config.mq.utils.SaasMqUtils;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Log4j2
@RestController
@RequestMapping("/app/cache")
public class CacheTestController {

    @Autowired
    private JetCacheTestService jetCacheTestService;

    @CreateCache(name = "myServiceCache:", cacheType = CacheType.REMOTE, expire = 60)
    private Cache<String, String> cache;




//    @GetMapping("/get99/{id}")
//    public BaseResult get99(@PathVariable int id) {
//
//        FastjsonKeyConvertor.INSTANCE
//        return BaseResult.ok();
//    }



//
//
//    @GetMapping("/get/{id}")
//    public BaseResult get(@PathVariable int id) {
//        System.out.println("CacheTestController.get");
//        CacheTestModel entity= jetCacheTestService.get(id);
//        return BaseResult.ok(entity);
//    }
//
//    @GetMapping("/get2/{id}")
//    public BaseResult get2(@PathVariable int id) {
//        System.out.println("CacheTestController.get222222222222");
//        CacheTestModel param=new CacheTestModel(id,"name::::"+id,"wwwwwwwww");
//        CacheTestModel entity= jetCacheTestService.get2(param);
////        FastjsonValueEncoded
////        FastjsonKeyConvertor
////        JSON.toJSONString(originalKey);
//        return BaseResult.ok(entity);
//    }

//    @GetMapping("/del/{id}")
//    public BaseResult del(@PathVariable int id) {
//        jetCacheTestService.del(id);
//        return BaseResult.ok();
//    }
//

    @GetMapping("/demo1/{id}")
    public BaseResult demo1(@PathVariable int id) {
        String key="Key:"+id;
        cache.put(key, "myValue"+id);
        String myValue = cache.get(key);
        return BaseResult.ok(myValue);
    }

    @GetMapping("/demo2/{id}")
    public BaseResult demo2(@PathVariable int id) {
        String key="Key:"+id;
        cache.remove(key);
        return BaseResult.ok();
    }




}
